<?php
	
/*
*------------------------------------------------------------------
*WordPress Uikit Navwalker
*-------------------------------------------------------------------
*/
	
// 设置菜单的参数
add_filter( 'wp_nav_menu_args', 'filter_wp_menu_args_header' );
function filter_wp_menu_args_header( $args ) {
	if ( $args['theme_location'] === 'primary' ) {
		$args['container']   = false;
		$args['items_wrap']  = '<ul class="%2$s">%3$s</ul>';
		$args['menu_class']  = 'uk-navbar-nav';
	}
	else if ( $args['theme_location'] === 'mobile' ) {
		$args['container']   = false;
		$args['items_wrap']  = '<ul class="%2$s" uk-nav>%3$s</ul>';
		$args['menu_class']  = 'uk-nav-default uk-nav-parent-icon';
	}
	return $args;
}
// 设置li标签的ld值
add_filter( 'nav_menu_item_id', 'filter_menu_item_css_id_header', 10, 4 );
function filter_menu_item_css_id_header( $menu_id, $item, $args, $depth ) {	
	return $args->theme_location === 'primary||mobile' ? '' : '';
}
// 设置li标签的class值
add_filter( 'nav_menu_css_class', 'filter_nav_menu_css_classes_header', 10, 4 );
function filter_nav_menu_css_classes_header( $classes, $item, $args, $depth ) {
	
	if ( $args->theme_location === 'primary' ) {
		$classes = [];
	}
 	else if ( $args->theme_location === 'mobile' ) {
		if ($classes != '' && in_array('menu-item-has-children', $classes) && $depth == 0){
			$classes = ['uk-parent'];
		} else {$classes = [];}

	}
	//$classes[] = 'uk-active';
	return $classes;
}
// 设置ul标签的class值
add_filter( 'nav_menu_submenu_css_class', 'filter_nav_menu_submenu_css_class_header', 10, 3 );
function filter_nav_menu_submenu_css_class_header( $classes, $args, $depth ) {
	if ( $args->theme_location === 'primary' ) {
		$classes = ['uk-nav uk-navbar-dropdown-nav',];
	}
 else if ( $args->theme_location === 'mobile' ) {
		$classes = ['uk-nav-sub',];
	}
	return $classes;
}
// 给a标签添加class和data属性
add_filter( 'nav_menu_link_attributes', 'filter_nav_menu_link_attributes_header', 10, 4 );
function filter_nav_menu_link_attributes_header( $atts, $item, $args, $depth ) {
	if ( $args->theme_location === 'primary||mobile' ) {
		$atts['class'] = '';
		if ( $item->current ) {
			$atts['class'] .= '';
		}
	}
	return $atts;
}


class Uikit_Walker_Nav_Menu extends Walker_Nav_Menu {
	public function start_lvl( &$output, $depth = 0, $args = array() ) {
		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
			$t = '';
			$n = '';
		} else {
			$t = "\t";
			$n = "\n";
		}
		$indent = str_repeat( $t, $depth );
		$classes = array( 'sub-menu' );
		$class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
		$output .= "{$n}{$indent}<div class='uk-navbar-dropdown' uk-drop='offset: -2;'><ul$class_names>{$n}";
	}
	public function end_lvl( &$output, $depth = 0, $args = array() ) {
		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
			$t = '';
			$n = '';
		} else {
			$t = "\t";
			$n = "\n";
		}
		$indent = str_repeat( $t, $depth );
		$output .= "$indent</ul></div>{$n}";
	}
}

class Uikit_Walker_Nav_Mobile_Menu extends Walker_Nav_Menu {
	public function start_lvl( &$output, $depth = 0, $args = array() ) {
		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
			$t = '';
			$n = '';
		} else {
			$t = "\t";
			$n = "\n";
		}
		$indent = str_repeat( $t, $depth );
		$classes = array( 'sub-menu' );
		$class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
		$output .= "{$n}{$indent}<ul$class_names>{$n}";
	}
	public function end_lvl( &$output, $depth = 0, $args = array() ) {
		if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
			$t = '';
			$n = '';
		} else {
			$t = "\t";
			$n = "\n";
		}
		$indent = str_repeat( $t, $depth );
		$output .= "$indent</ul>{$n}";
	}
}